链接：https://leetcode-cn.com/problems/binary-search/solution/er-fen-cha-zhao-by-leetcode/

class Solution {
  public:
  int search(vector<int>& nums, int target)
  {
    int pivot;
    int left = 0;
    int right = nums.size() - 1;

    // 循环
    while (left <= right)
    {
      // 取中间 index
      pivot = left + (right - left) / 2;

      // 如果找到元素
      if (nums[pivot] == target)
        return pivot;

      if (target < nums[pivot])
        right = pivot - 1; // 往【左】边继续查找
      else
        left = pivot + 1; // 往【右】边继续查找
    }

    // 未找到
    return -1;
  }
};
